package com.bwie.filter;

import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;


/**
 * @description : 全局过滤器
 * @Author QiHainan
 * @Create 2025/3/25 9:29
 * @Version 1.0
 */
@Order(-1)
@Configuration
public class MyGlobalFilter implements GlobalFilter {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String url = request.getURI().getPath().toString();
        System.out.println("url:"+url);
        //白名单
        if(url.contains("/login")){
            System.out.println("白名单："+url);
            return chain.filter(exchange);
        }
        if(url.contains("/uploadImage")){
            System.out.println("白名单："+url);
            return chain.filter(exchange);
        }
        //检查token
        String token = request.getHeaders().getFirst("token");
        if(token == null){
            System.out.println("token为空："+url);
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);

        String tokenKey = "token-"+userId;
        String redisToken = redisTemplate.opsForValue().get(tokenKey);
        if(redisToken == null){
            System.out.println("token已失效："+url);
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        if(!token.equals(redisToken)){
            System.out.println("token错误："+url);
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        //token续期
        redisTemplate.expire(tokenKey,60, TimeUnit.MINUTES);
        //放行
        System.out.println("放行："+url);
        return chain.filter(exchange);

//        //获取参数
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//        String url = request.getURI().getPath().toString();
//        System.out.println("url:"+url);
//        //白名单
//        if(url.contains("/login")){
//            System.out.println("白名单："+url);
//            return chain.filter(exchange);
//        }
//        if(url.contains("/uploadImage")){
//            System.out.println("白名单："+url);
//            return chain.filter(exchange);
//        }
//        //检查token
//        String token = request.getHeaders().getFirst("token");
//        if(token == null){
//            System.out.println("token为空："+url);
//            //HttpStatus.UNAUTHORIZED == 错误代码是401
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//
//        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);
//        String tokenKey = "token-"+userId;
//        String redisToken = redisTemplate.opsForValue().get(tokenKey);
//        if(redisToken == null){
//            System.out.println("token已经过期了"+url);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        if(!token.equals(redisToken)){
//            System.out.println("token错误了"+url);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //token续期
//        redisTemplate.expire(tokenKey,60, TimeUnit.MINUTES);
//        //放行
//        System.out.println("放行"+url);
//        return chain.filter(exchange);
    }
}
